package com.woniuxy.java102vuecli.service.impl;

import com.woniuxy.java102vuecli.entity.Permission;
import com.woniuxy.java102vuecli.mapper.PermissionMapper;
import com.woniuxy.java102vuecli.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author author
 * @Create 2023-10-10 16:14
 */
@Slf4j
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Override
    public List<String> getPermissionPathsByEid(int eid) {
        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
        if(redisTemplate.hasKey("permission:"+eid)){
            log.info("redis缓存获取权限");
            return (List<String>)opsForValue.get("permission:"+eid);
        }else{
            log.info("db数据库查询权限");
            List<Permission> permissions= permissionMapper.selectPermissionsByEid(eid);
            List<String> paths=permissions.stream().map(permission -> permission.getPath()).collect(Collectors.toList());
            opsForValue.set("permission:"+eid, paths);
            return paths;
        }

    }
}
